{template '_header'}
<div class="page-heading"> 
    <span class='pull-right'>
        {ifp 'shop.dispatch.add'}
        	<a class="btn btn-primary btn-sm" href="{php echo merchUrl('shop/dispatch',array('op'=>'post'))}">添加新配送方式</a>
        {/if}
        <a class="btn btn-default  btn-sm" href="{php echo merchUrl('shop/dispatch')}">返回列表</a>
    </span>
    <h2>{if !empty($dispatch['id'])}编辑{else}添加{/if}配送方式 <small>{if !empty($dispatch['id'])}修改【{$dispatch['dispatchname']}】{/if}</small></h2> 
</div>


<form {ife 'shop.dispatch' $dispatch}action="" method="post"{/if} class="form-horizontal form-validate" enctype="multipart/form-data">
    <input type="hidden" name="id" value="{$dispatch['id']}" />
    <div class="form-group">
        <label class="col-sm-2 control-label">排序</label>
        <div class="col-sm-9 col-xs-12">
            {ife 'shop.dispatch' $dispatch}
            	<input type="text" name="displayorder" class="form-control" value="{$dispatch['displayorder']}" />
            {else}
            	<div class='form-control-static'>{$dispatch['displayorder']}</div>
            {/if}
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label must">配送方式名称</label>
        <div class="col-sm-9 col-xs-12">
            {ife 'shop.dispatch' $dispatch}
            	<input type="text" id='dispatchname' name="dispatchname" class="form-control" value="{$dispatch['dispatchname']}" data-rule-required='true' />
            {else}
            	<div class='form-control-static'>{$dispatch['dispatchname']}</div>
            {/if}

        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label ">是否默认</label>

        <div class="col-sm-9 col-xs-12">
            {ife 'shop.dispatch' $dispatch}
	            <label class='radio-inline'>
	                <input type='radio' name='isdefault' id="isdefault1" value='1' {if $dispatch['isdefault']==1}checked{/if} /> 是
	            </label>
	            <label class='radio-inline'>
	                <input type='radio' name='isdefault' id="isdefault0" value='0' {if $dispatch['isdefault']==0}checked{/if} /> 否
	            </label>
            {else}
            	<div class='form-control-static'>{if empty($dispatch['calculatetype'])}按重量计费{else}按件计费{/if}</div>
            {/if}
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label ">计费方式</label>

        <div class="col-sm-9 col-xs-12">
            {ife 'shop.dispatch' $dispatch}
	            <label class='radio-inline'>
	                <input type='radio' name='calculatetype' value='0' {if $dispatch['calculatetype']==0}checked{/if} /> 按重量计费
	            </label>
	            <label class='radio-inline'>
	                <input type='radio' name='calculatetype' value='1' {if $dispatch['calculatetype']==1}checked{/if} /> 按件计费
	            </label>
            {else}
            	<div class='form-control-static'>{if empty($dispatch['calculatetype'])}按重量计费{else}按件计费{/if}</div>
            {/if}
        </div>
    </div>

    <div class="form-group dispatch0">
        <label class="col-sm-2 control-label ">物流公司</label>
        <div class="col-sm-9 col-xs-12">
            {ife 'shop.dispatch' $dispatch}
	            <input type='hidden' name='expressname' value='{$dispatch['expressname']}'/>
	                   <select name='express' class="form-control select2">
	                <option value="" data-name="其他快递">其他快递</option>
	
	               {loop $express_list $value}
	                   <option value="{$value['express']}" data-name="{$value['name']}">{$value['name']}</option>
	               {/loop}
	
	            </select>
	            <span class="help-block">如果您选择了常用快递，则客户可以订单中查询快递信息，如果缺少您想要的快递，您可以联系我们! </span>
            {else}
            	<div class='form-control-static'>{$dispatch['expressname']}</div>
            {/if}
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label ">配送区域</label>
        <div class="col-sm-9 col-xs-12">

            <table  style='width:100%;'>
                <thead>
                    <tr>
                        <th style="height:40px;width:400px;">运送到</th>
                        <th class="show_h" style="width:110px;">首重(克)</th>
                        <th class="show_h" style="width:110px;">首费(元)</th>
                        <th class="show_h" style="width:110px;">续重(克)</th>
                        <th class="show_h" style="width:110px;">续费(元)</th>

                        <th class="show_n" style="width:110px;">首件(个)</th>
                        <th class="show_n" style="width:110px;">运费(元)</th>
                        <th class="show_n" style="width:110px;">续件(个)</th>
                        <th class="show_n" style="width:110px;">续费(元)</th>
                        <th style="width:80px;">管理</th>
                    </tr>
                </thead>
                <tbody id='tbody-areas'>
                    <tr>
                        <td style="padding:10px;">全国 [默认运费]</td>
                        <td class="show_h text-center">
                            {ife 'shop.dispatch' $dispatch}
                            	<input type="text" value="{php echo empty($dispatch['firstweight'])?1000:$dispatch['firstweight']}" class="form-control" name="default_firstweight" style="width:100px;"></td>
                            {else}
                            	<div class='show_h form-control-static'>{php echo empty($dispatch['firstweight'])?1000:$dispatch['firstweight']}</div>
                            {/if}
                		<td class="show_h text-center">
                			{ife 'shop.dispatch' $dispatch}
                				<input type="text" value="{$dispatch['firstprice']}" class="form-control" name="default_firstprice"  style="width:100px;"></td>
                			{else}
                				<div class='show_h form-control-static'>{$dispatch['firstprice']}</div>
                			{/if}
                			<td class="show_h text-center">
                				{ife 'shop.dispatch' $dispatch}
                					<input type="text" value="{php echo empty($dispatch['secondweight'])?1000:$dispatch['secondweight']}" class="form-control" name="default_secondweight"  style="width:100px;">
                				{else}
                					<div class='show_h form-control-static'>{php echo empty($dispatch['secondweight'])?1000:$dispatch['secondweight']}</div>
                				{/if}
                			</td>
                			<td class="show_h text-center">
                				{ife 'shop.dispatch' $dispatch}
                					<input type="text" value="{$dispatch['secondprice']}" class="form-control" name="default_secondprice"  style="width:100px;">
                				{else}
                					<div class='show_h form-control-static'>{$dispatch['secondprice']}</div>
                				{/if}
                			</td>
                			<td class="show_h"></td>
                			<td class="show_n text-center">
                				{ife 'shop.dispatch' $dispatch}
                					<input type="text" value="{php echo empty($dispatch['firstnum'])?1:$dispatch['firstnum']}" class="form-control" name="default_firstnum" style="width:100px;">
                				{else}
                					<div class='show_n form-control-static'>{php echo empty($dispatch['firstnum'])?1:$dispatch['firstnum']}</div>
                				{/if}
                			</td>
                			<td class="show_n text-center">
                				{ife 'shop.dispatch' $dispatch}
                					<input type="text" value="{$dispatch['firstnumprice']}" class="form-control" name="default_firstnumprice"  style="width:100px;">
                				{else}
                					<div class='show_n form-control-static'>{$dispatch['firstnumprice']}</div>
                				{/if}
                			</td>
                			<td class="show_n text-center">
                				{ife 'shop.dispatch' $dispatch}
                					<input type="text" value="{php echo empty($dispatch['secondnum'])?1:$dispatch['secondnum']}" class="form-control" name="default_secondnum"  style="width:100px;">
                				{else}
                					<div class='show_n form-control-static'>{php echo empty($dispatch['secondnum'])?1:$dispatch['secondnum']}</div>
                				{/if}
                			</td>
                			<td class="show_n text-center">
                				{ife 'shop.dispatch' $dispatch}
                					<input type="text" value="{$dispatch['secondnumprice']}" class="form-control" name="default_secondnumprice"  style="width:100px;">
                				{else}
                					<div class='show_n form-control-static'>{$dispatch['secondnumprice']}</div>
                				{/if}
                			</td>
                			<td class="show_n"></td>
                    </tr>
                {loop $dispatch_areas $row}
	                {php $random = random(16);}
	                {template 'shop/dispatch/tpl'}
                {/loop}
                </tbody>
            </table>
            {ife 'shop.dispatch' $dispatch}
	            <a class='btn btn-default' href="javascript:;" onclick='addArea(this)'><span class="fa fa-plus"></span> 新增配送区域</a>
	            <span class='help-block show_h' {if $dispatch['calculatetype']==1}style='display:none'{/if}>根据重量来计算运费，当物品不足《首重重量》时，按照《首重费用》计算，超过部分按照《续重重量》和《续重费用》乘积来计算</span>
	            <span class='help-block show_n' {if $dispatch['calculatetype']==0}style='display:none'{/if}>根据件数来计算运费，当物品不足《首件数量》时，按照《首件费用》计算，超过部分按照《续件重量》和《续件费用》乘积来计算</span>
            {/if}


        </div>
    </div>


<div class="form-group">
        <label class="col-sm-2 control-label ">状态</label>
        <div class="col-sm-9 col-xs-12">
            {ife 'shop.dispatch' $dispatch}
	            <label class='radio-inline'>
	                <input type='radio' name='enabled' value=1' {if $dispatch['enabled']==1}checked{/if} /> 启用
	            </label>
	            <label class='radio-inline'>
	                <input type='radio' name='enabled' value=0' {if $dispatch['enabled']==0}checked{/if} /> 禁用
	            </label>
            {else}
            	<div class='form-control-static'>{if empty($item['enabled'])}禁用{else}启用{/if}</div>
            {/if}
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label "></label>
        <div class="col-sm-9 col-xs-12">
            {ife 'shop.dispatch' $dispatch}
            	<input type="submit" value="提交" class="btn btn-primary"  />
            {/if}
            <input type="button" name="back" onclick='history.back()' {ifp 'shop.dispatch.add|shop.dispatch.edit'}style='margin-left:10px;'{/if} value="返回列表" class="btn btn-default" />
        </div>
    </div>


</div>
</div>

</form>
</div>
{template 'shop/selectareas'}

<script language='javascript'>
    function show_type(flag){
    if (flag == 1) {
    $('.show_h').hide();
    $('.show_n').show();
    } else {
    $('.show_h').show();
    $('.show_n').hide();
    }
    }
    $(function(){
    show_type({php echo $dispatch['calculatetype']});
    $(':radio[name=calculatetype]').click(function(){
    var val = $(this).val();
    show_type(val);
    })
            $(':radio[name=dispatchtype]').click(function(){
    var val = $(this).val();
    $(".dispatch0,.dispatch1").hide();
    $(".dispatch" + val).show();
    })

            $("select[name=express]").change(function(){
    var obj = $(this);
    var sel = obj.find("option:selected");
    $(":input[name=expressname]").val(sel.data("name"));
    });
    {if !empty($dispatch['express'])}
    $("select[name=express]").val("{$dispatch['express']}");
    {/if}


            $('.province').mouseover(function(){
    $(this).find('ul').show();
    }).mouseout(function(){
    $(this).find('ul').hide();
    });
    $('.cityall').click(function(){
    var checked = $(this).get(0).checked;
    var citys = $(this).parent().parent().find('.city');
    citys.each(function(){
    $(this).get(0).checked = checked;
    });
    var count = 0;
    if (checked){
    count = $(this).parent().parent().find('.city:checked').length;
    }
    if (count > 0){
    $(this).next().html("(" + count + ")");
    }
    else{
    $(this).next().html("");
    }
    });
    $('.city').click(function(){
    var checked = $(this).get(0).checked;
    var cityall = $(this).parent().parent().parent().parent().find('.cityall');
    if (checked){
    cityall.get(0).checked = true;
    }
    var count = cityall.parent().parent().find('.city:checked').length;
    if (count > 0){
    cityall.next().html("(" + count + ")");
    }
    else{
    cityall.next().html("");
    }
    });
    });
    function getCurrents(withOutRandom){
    var citys = "";
    $('.citys').each(function(){
    var crandom = $(this).prev().val();
    if (withOutRandom && crandom == withOutRandom){
    return true;
    }
    citys += $(this).val();
    });
    return citys;
    }

    var current = '';
    function addArea(btn){
    $(btn).button('loading');
    $.ajax({
    url:"{php echo merchUrl('shop/dispatch/tpl')}",
            dataType:'json',
            success:function(json){
            $(btn).button('reset');
            current = json.random;
            $('#tbody-areas').append(json.html);
            $('#tbody-areas tr').last().hide();
            clearSelects();
            $("#modal-areas").modal();
            var currents = getCurrents();
            currents = currents.split(';');
            var citystrs = "";
            $('.city').each(function(){
            var parentdisabled = false;
            for (var i in currents){
            if (currents[i] != '' && currents[i] == $(this).attr('city')){
            $(this).attr('disabled', true);
            $(this).parent().parent().parent().parent().find('.cityall').attr('disabled', true);
            }
            }

            });
            $('#btnSubmitArea').unbind('click').click(function(){
            $('.city:checked').each(function(){
            citystrs += $(this).attr('city') + ";";
            });
            $('.' + current + ' .cityshtml').html(citystrs);
            $('.' + current + ' .citys').val(citystrs);
            $('#tbody-areas tr').last().show();
            })
                    var calculatetype1 = $('input[name="calculatetype"]:checked ').val();
            show_type(calculatetype1);
            }
    })
    }

</script>
{template '_footer'}